#kubectl rollout history deploy/jenkins-demo -n <namespace> | grep -v "deployment" | grep -v "REVISION" | awk '{print $1}' > version.csv

#!/bin/bash

# 部署名称和命名空间
DEPLOYMENT="jenkins-demo"
NAMESPACE="<namespace>"
CSV_FILE="version.csv"

# 获取历史版本并生成 CSV 文件
echo "Fetching rollout history for deployment $DEPLOYMENT in namespace $NAMESPACE..."

# 清空 CSV 文件并写入标题
echo "Revision" > "$CSV_FILE"

# 获取所有版本
VERSIONS=$(kubectl rollout history deployment "$DEPLOYMENT" -n "$NAMESPACE" | grep -v "deployment" | grep -v "REVISION" | awk '{print $1}')

# 遍历版本并提取镜像
for VERSION in $VERSIONS; do
    # 获取版本详情，并提取镜像
    IMAGE=$(kubectl rollout history deployment "$DEPLOYMENT" --revision="$VERSION" -n "$NAMESPACE" | grep "Image:" | awk '{print $2}')
    
    # 打印镜像信息
    echo "Version $VERSION uses image: $IMAGE"
    
    # 写入 CSV，仅包含版本号
    echo "$VERSION" >> "$CSV_FILE"
done

echo "Version history saved to $CSV_FILE."
cat "$CSV_FILE"
